<?php

namespace App\Admin\Metrics\Examples;

use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Bar;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Models\Order;

class OrderMoney extends Bar
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $color = Admin::color();

        $dark35 = $color->dark35();

        // 卡片内容宽度
        $this->contentWidth(5, 7);
        // 标题
        $this->title('订单金额￥');
        // 设置下拉选项
        $this->dropdown([
            '1' => '今日',
            '7' => '最近 7 天',
            '10' => '最近 10 天',
            '30' => '最近 30 天',
            '100' => '最近 100 天',
        ]);
        // 设置图表颜色
        $this->chartColors([
            $color->primary(),
            $color->primary(),
            $color->primary(),
            $color->primary(),
            $color->primary(),
            $color->primary(),
            $color->primary()
        ]);

        // $this->chartLabels([75, 125, 225, 175, 125, 75, 25]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $days = $request->get('option')??1;
        $today = $date = Carbon::today();
        if ($days != 1) {
            $date = Carbon::today()->addDays(1-$days);
        }
        $total = Order::paid()->whereDate('paid_at', '>=', $date)->sum('price');
        $today = Order::paid()->whereDate('paid_at', '>=', $today)->sum('price');
        $lastday = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-1))->whereDate('paid_at', '<', Carbon::today())->sum('price');
        $lastday2 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-2))->whereDate('paid_at', '<', Carbon::today()->addDays(-1))->sum('price');
        $lastday3 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-3))->whereDate('paid_at', '<', Carbon::today()->addDays(-2))->sum('price');
        $lastday4 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-4))->whereDate('paid_at', '<', Carbon::today()->addDays(-3))->sum('price');
        $lastday5 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-5))->whereDate('paid_at', '<', Carbon::today()->addDays(-4))->sum('price');
        $lastday6 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-6))->whereDate('paid_at', '<', Carbon::today()->addDays(-5))->sum('price');


        // 卡片内容
        $this->withContent($total, $request->get('option')??1);

        // 图表数据
        $this->withChart([
            [
                'name' => '日金额',
                'data' => [$today, $lastday,$lastday2, $lastday3, $lastday4,$lastday5],
                // 'label' => [$today, $lastday,$lastday2, $lastday3, $lastday4,$lastday5],
            ],
        ]);
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data,
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $title
     * @param string $value
     * @param string $style
     *
     * @return $this
     */
    public function withContent($title, $day, $style = 'success')
    {
        // 根据选项显示
        $label = strtolower(
            $day==1 ? '今日': "最近 {$day} 天"
        );

        $minHeight = '183px';

        return $this->content(
            <<<HTML
<div class="d-flex p-1 flex-column justify-content-between" style="padding-top: 0;width: 100%;height: 100%;min-height: {$minHeight}">
    <div class="text-left">
        <h1 class="font-lg-2 mt-2 mb-0">{$title}</h1>
        <h5 class="font-medium-2" style="margin-top: 10px;">
            <span class="text-{$style}">{$label} </span>
            <span>总金额</span>
        </h5>
    </div>

    <a href="#" class="btn btn-primary shadow waves-effect waves-light">右侧最近7日每天金额<i class="feather icon-chevrons-right"></i></a>
</div>
HTML
        );
    }
}
